using System;
using Godot;
using Box.Base;

namespace Box.GameCore {
    /*
        存储结构

        root
            L saves 存档目录
                L [save_name]
                    L blocks
                    L sandbox.dat
            L modes 模组目录
    */
    public class StorageSystem {
        public const string SAVES_NAME = "save"; 
        public const string SAVES_PATH = $"user://{SAVES_NAME}";
        public const string ARCHIVE_INFO_FILE_NAME = "archive.dat";


        public Archive CurrentArchive {get;protected set;}

        public Archive CreateArchive(string name) {
            if(HasArchive(name)) return null;
            Archive archive = new Archive(name,$"{SAVES_PATH}/{name}");
            GDIOStorageFile info_file = new GDIOStorageFile();
            info_file.Open($"{archive.Path}/{ARCHIVE_INFO_FILE_NAME}",StorageFileMode.Write);

            info_file.Write(archive);

            DirAccess.MakeDirAbsolute(archive.Path);
            return archive;
        }

        public Archive OpenArchive(string name) {
            Archive archive = GetArchive(name);
            if(archive == null) return null;
            CurrentArchive = archive;
            return archive;
        }

        public Archive GetArchive(string name) {
            if(!HasArchive(name)) return null;
            GDIOStorageFile archive_info = new GDIOStorageFile();
            string path = $"{SAVES_PATH}/{name}";
            archive_info.Open(path,StorageFileMode.Read);
            //...
            
            Archive archive = new Archive(name,path);
            return archive;
        }

        public bool HasArchive(string name) {
            return DirAccess.DirExistsAbsolute($"{SAVES_PATH}/{name}");
        }
    }
}